package com.anxin.act.process.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anxin.act.task.service.ActTaskService;
import com.anxin.act.task.vo.TaskVo;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.sys.system.entity.SysUser;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 自由流相关Service
 * @author: liuxiangyu
 * @date: 2020/4/26 15:27
 */
@Service
public class ActSelfBuiltService {

    @Autowired
    private ActTaskService actTaskService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    /**
     * 自由流启动时审批人设置
     * @param procInsId
     * @param nextApply
     */
    public void startApproval(String procInsId, JSONArray nextApply){
        //将用户id转换为登录名
        JSONArray nextUserIdApply = new JSONArray();
        for (int i = 0; i < nextApply.size(); i++) {
            JSONObject nextUserIdObject = nextApply.getJSONObject(i);
            nextUserIdApply.add(nextUserIdObject.getString("user"));
        }

        JSONArray userNameArray = new JSONArray();
        for (int i = 0; i < nextUserIdApply.size(); i++) {
            JSONArray jsonArray = this.userIdStringToArray(nextUserIdApply.getString(i));
            userNameArray.add(UserUtils.idToLoginName(jsonArray));
        }


        // 设置流程变量
        this.settingProcessVariables(procInsId,userNameArray);


    }

    /**
     * 获取自由流接下来的审批人
     * @param taskVo
     * @return
     */
    public void getNextApproval(TaskVo taskVo){
        taskVo.setNextApprovalArray(new JSONArray());
        if ("act_free_flow_approval".equals(taskVo.getProcessDefKey())){
            Object freeFlowAssignees = runtimeService.getVariable(taskVo.getActId(), "freeFlowAssignees");
            if (freeFlowAssignees != null){
                JSONArray freeFlowAssigneesArray = JSONObject.parseArray(freeFlowAssignees.toString());
                for (int i = 0; i < freeFlowAssigneesArray.size(); i++) {
                    String nextApproval = freeFlowAssigneesArray.getString(i);
                    SysUser sysUser = UserUtils.getUserByUserName(nextApproval);
                    if (sysUser != null){
                        taskVo.getNextApprovalArray().add(sysUser.getNickName());
                    }
                }
            }
        }
    }

    /**
     * 用户Id的字符串转为Array
     * @return
     */
    private JSONArray userIdStringToArray(String userId){
        String[] userIds = userId.replace("]","").replace("[","").split(", ");
        JSONArray userIdArray = new JSONArray();
        userIdArray.addAll(Arrays.asList(userIds));
        return userIdArray;
    }

    /**
     * 设置流程变量
     * @param procInsId 流程实例ID
     * @param userArray 用户集合
     */
    private void settingProcessVariables(String procInsId ,JSONArray userArray){
        // 判断是单人还是会签
        //runtimeService.setVariable(procInsId, "selfBuiltSign", userArray.getJSONArray(0).size() > 1);
        // 下一个节点的审批人
        //runtimeService.setVariable(procInsId, "selfBuiltAssignees", Arrays.asList(userArray.getJSONArray(0).toArray()));

        //选择的全部审批人
        runtimeService.setVariable(procInsId, "selfBuiltAssigneesAll", userArray.toJSONString());
        //userArray.remove(0);
        //剩余未审的审批人
        runtimeService.setVariable(procInsId, "selfBuiltAssigneesRemaining", userArray.toJSONString());
    }

}
